package bigraph.model;

import java.util.ArrayList;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.views.properties.ColorPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

public class NodePropertySource implements IPropertySource {
    
	private Node node;
    
	public NodePropertySource(Node node) {
         this.node = node;
    }

	@Override
	public Object getEditableValue() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	 public IPropertyDescriptor[] getPropertyDescriptors() {
	    ArrayList<IPropertyDescriptor> properties = new ArrayList<IPropertyDescriptor>();
//	    if (node instanceof Root)
	//          properties.add(new TextPropertyDescriptor(Node.PROPERTY_RENAME, "Name"));
	  //  else
	     properties.add(new TextPropertyDescriptor(Node.PROPERTY_RENAME, "Name"));
	     if (node instanceof Site) {
	          properties.add(new ColorPropertyDescriptor(Site.PROPERTY_COLOR, "Color"));
	         // properties.add(new TextPropertyDescriptor(Node.PROPERTY_RENAME, "Name"));
	     }
	     else if (node instanceof Place) {
	          properties.add(new ColorPropertyDescriptor(Place.PROPERTY_COLOR, "Color"));
	     }
	     return properties.toArray(new IPropertyDescriptor[0]);
	}


	@Override
	public Object getPropertyValue(Object id) {
	     if (id.equals(Node.PROPERTY_RENAME))
	            return node.getName();
	     if (id.equals(Site.PROPERTY_COLOR))
	    	 	return ((Site)node).getColor().getRGB();
	     if (id.equals(Place.PROPERTY_COLOR))
	    	 	return ((Place)node).getColor().getRGB();
	    /*
	     if (id.equals(Service.PROPERTY_FLOOR))
	            return Integer.toString(((Service)node).getEtage());
	     if (id.equals(Entreprise.PROPERTY_CAPITAL))
	            return Integer.toString(((Entreprise)node).getCapital());
	     if (id.equals(Employe.PROPERTY_FIRSTNAME))
	            return (((Employe)node).getPrenom());  
	            */
	     return null;
	}


	// Returns if the property with the given id has been changed since its initial default value.
	// We do not handle default properties, so we return <tt>false</tt>.
	@Override
	public boolean isPropertySet(Object id) {
	     return false;
	}

	@Override
	public void resetPropertyValue(Object id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setPropertyValue(Object id, Object value) {
	     if (id.equals(Node.PROPERTY_RENAME))
	            node.setName((String)value);
	     else if (id.equals(Site.PROPERTY_COLOR)) {
	            Color newColor = new Color(null, (RGB)value);
	            ((Site)node).setColor(newColor);
	     }
	   	 else if (id.equals(Place.PROPERTY_COLOR)) {
                Color newColor2 = new Color(null, (RGB)value);
	            ((Place)node).setColor(newColor2);	            
	     }
	     /*
	     else if (id.equals(Service.PROPERTY_FLOOR)) {
	            try {
	                  Integer floor = Integer.parseInt((String)value);
	                  ((Service)node).setEtage(floor);
	            }
	            catch (NumberFormatException e) { }
	     }
	     else if (id.equals(Entreprise.PROPERTY_CAPITAL)) {
	            try {
	                  Integer capital = Integer.parseInt((String)value);
	                  ((Entreprise)node).setCapital(capital);
	            }
	            catch (NumberFormatException e) { }
			*/
	     }


}
